
import { _decorator, Component, Node, sp } from 'cc';
import { BaseItem2D } from '../BaseItem2D';
import { Main } from '../Main';
const { ccclass, property } = _decorator;


 
@ccclass('Bullet')
export class Bullet extends BaseItem2D {
    speed=10
    speedX=0
    speedY=0
    width=30
    height=30
    
    init(x,y,direction){
        this.setPos(x,y)
        direction.normalize()
        this.speedX=direction.x*this.speed
        this.speedY=direction.y*this.speed
        Main.I.bullets.push(this)
        Main.I.bulletPlace.addChild(this.node)
    }

    loop(){
        this.setPos(this.x+this.speedX,this.y+this.speedY)
        let monsters=Main.I.battleMap.testHitMonsters(this.x,this.y,this.width,this.height)
        if(monsters.length>0){
            monsters.forEach(monster=>{
                monster.die()
                this.node.destroy()
            })
            
        }
    }
    onDestroy(){
        let index=Main.I.bullets.indexOf(this)
        if(index>=0){
            Main.I.bullets.splice(index,1)
        }
    }
}

